package com.hakim.wfb.common.serial;

/**
 * Created by ShenLiang on 2017/6/26.
 */
public class TreeSerialGenerator {
   /*
   private static final char[] serials = new char[]{'1','2','3','4','5','6','7','8','9',
         'A','B','C','D','E','F','G',
         'H','I','J','K','L','M','N',
         'O','P','Q','R','S','T',
         'U','V','W','X','Y','Z',
         'a','b','c','d','e','f','g',
         'h','i','j','k','l','m','n',
         'o','p','q','r','s','t',
         'u','v','w','x','y','z'};
   */
    /**
     * 用于组织架构时的分割点
     */
    public static final String DEFAULT_SPLIT_CHAR = "_";

    /**
     * 判断字符c 是否属于 1-9 A-Z a-z
     *
     * @param c 被判断的字符
     * @return
     */
    public static boolean contains(char c) {
        if ((c >= 49 && c <= 57) || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
            return true;
        else
            return false;
    }

    /**
     * 获取字符串在serials中的位置，可以算出作为直推的人数
     *
     * @param currentSuffix
     * @return
     */
    public static int convertToCount(String currentSuffix) {
        if (currentSuffix == null || "".equals(currentSuffix) || "0".equals(currentSuffix))
            return 0;

        char[] array = currentSuffix.toCharArray();
        int index = 0;
        int count = 0;
        int currentCharIntValue = 0;
        for (char c : array) {
            if (contains(c) == false)
                return -1;

            if (c >= 49 && c <= 57)
                currentCharIntValue = c - 48;
            else if (c >= 'A' && c <= 'Z')
                currentCharIntValue = c - 55;
            else if (c >= 'a' && c <= 'z')
                currentCharIntValue = c - 61;
            int m = array.length - index;
            if (m == 1)
                count += currentCharIntValue;
            else
                count += currentCharIntValue * Math.pow(10, m);
            index++;
        }
        return count;
    }

    /**
     * 获取树形组织结构代码，子级下一个末端字符串
     * 如果不为空 "" null 0，当currentSuffix的最后一位值不在'1-9 A-Z a-z则返回""'
     *
     * @param currentSuffix 当前子级序列号末段字符串
     * @return 返回下一个序列号
     */
    public static String createNextSuffix(String currentSuffix) {
        char lastChar;
        String preStr = "";
        if (currentSuffix.length() > 1) {
            lastChar = currentSuffix.charAt(currentSuffix.length() - 1);
            preStr = currentSuffix.substring(0, currentSuffix.length() - 1);
        } else {
            lastChar = currentSuffix.charAt(0);
        }

        if (currentSuffix == null || "".equals(currentSuffix) || "0".equals(currentSuffix))
            return "1";
        else if ((lastChar >= 49 && lastChar < 57) || (lastChar >= 'A' && lastChar < 'Z') || lastChar >= 'a' && lastChar < 'z')
            return preStr + Character.toString((char) (lastChar + 1));
        else if (lastChar == 57)
            return preStr + 'A';
        else if (lastChar == 'Z')
            return preStr + 'a';
        else if (lastChar == 'z')
            return preStr + "11";
        else
            return "";
    }

    /**
     * 获取下一个子级序列号
     *
     * @param parent             父级序列号
     * @param currentChildSuffix 当前子级序列号
     * @return
     */
    public static String createNextChildSerial(String parent, String currentChildSuffix) {
        return parent + DEFAULT_SPLIT_CHAR + createNextSuffix(currentChildSuffix);
    }

    /**
     * 获取下一个子级序列号
     *
     * @param currentChildSerial 当前子级序列号
     * @return
     */
    public static String createNextChildSerial(String currentChildSerial) {
        return createNextSuffix(currentChildSerial);
    }

    /**
     * 获取下一子级第一个序列号
     *
     * @param parentSuffix
     * @return
     */
    public static String createFirstChildSerial(String parentSuffix) {
        return parentSuffix + DEFAULT_SPLIT_CHAR + "1";
    }

//    public static void main(String[] args) {
//
//        String currentSuffix = "1.A";
//        String nextSuffix = createNextSuffix(currentSuffix); //1.B
//        String nextChildSerial = createNextChildSerial(currentSuffix);
//
//        String nextChildSerialSuff = createNextChildSerial(currentSuffix, nextSuffix);
//        System.out.println(nextSuffix);
//        System.out.println(nextChildSerial);
//        System.out.println(nextChildSerialSuff);
//
//    }
}